<?php
    namespace Home\Controller;
    use Think\Controller;
    class JixiaoshenheController extends Controller {
    
                public function _initialize()
    {
        header("Access-Control-Allow-Origin: *");  

        header("Access-Control-Allow-Credentials: true"); 
      
        header("Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS"); //允许的请求类型
        
        header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept"); 

    }

    public function get_list()
    {
        $page = I("get.page");
        $limit = I("get.limit");

        $guide_type = I("get.guide_type");
        $account_date = I("get.account_date");
        $account_date = strtotime($account_date);

        $sql = "SELECT a.*,b.dept_name,b.dept_code
  FROM performance.bonus_value a,
       PERFORMANCE.BONUS_INDEX c,
       COMM.SYS_DEPT_DICT b
 WHERE     a.guide_code IN
              (SELECT guide_code
                 FROM performance.set_bonusguide_gather
                WHERE     guide_type = '$guide_type'
                      AND account_date = '$account_date'
                      AND sort != 0
                      AND sort IS NOT NULL)
       AND A.UNIT_type = '$guide_type'
       AND A.INDEX_ID = c.id
       AND C.ACCOUNT_DATE = '$account_date'
       AND a.unit_code = b.dept_code";
       $data = M()->query($sql);

       $list = M('performance.set_bonusguide_gather')->where("guide_type = '$guide_type' and account_date = '$account_date' AND sort != 0
                      AND sort IS NOT NULL")->select();
        $col_list = [];
        $item['title'] = "科室";
        $item['code'] = "科室";
        $col_list[] = $item;
        for ($i=0; $i < count($list); $i++) 
        {
            $item['title'] = $list[$i]['guide_name'];
            $item['code'] = $list[$i]['guide_code'];
            $col_list[] = $item;
        }

        unset($item);
        $arr = [];
        $keshi_list = [];

        for ($i=0; $i < count($data); $i++) 
        { 
            //var_dump(in_array($data[$i]["guide_code"],$keshi_list));
            if(!in_array($data[$i]["dept_code"],$keshi_list))
            {
                $keshi_list[] = $data[$i]["dept_code"];

                $item["dept_code"] = $data[$i]["dept_code"];
                $item["科室"] = $data[$i]["dept_name"];
                $arr[] = $item;
            }
        }

        
        for ($i=0; $i < count($data); $i++) 
        { 
            
            for ($j=0; $j < count($col_list); $j++) 
            {
                
                for ($k=0; $k < count($arr); $k++) 
                { 
                    if($arr[$k]["dept_code"] == $data[$i]['dept_code'])
                    {
                        if($col_list[$j]["code"] == $data[$i]['guide_code'])
                        {
                            $arr[$k][$data[$i]['guide_code']] = $data[$i]['guide_value'];
                        }
                    }
                }
            }
        }

        $json["data"] = $arr;
        $json["code"] = 0;
        $json["count"] = 0;
        $json["msg"] = "";
        
        $this->ajaxReturn($json,"json");
    }

    public function get_one()
    {
        $uuid = I("post.uuid");

        $info = M("performance.bonus_index")->where("uuid = '$uuid'")->find();
        $info["dept_date"] = date("Y-m-d H:i:s",$info["dept_date"]);
        $json["data"]   = $info;
        $json["code"]   = 0;
        $json["msg"]    = "";

        $this->ajaxReturn($json,"json");
    }

    public function save_one()
    {
        $data = I("post.");
        $uuid = $data["uuid"];
        unset($data["uuid"]);

        $result = M("performance.bonus_index")->where("uuid = '$uuid'")->save($data);
        if($result !== false)
        {
            $json["code"] = 0;
            $json["msg"] = "修改成功";
        }
        else
        {
            $json["code"] = -1;
            $json["msg"] = "修改失败";
        }
 
        $this->ajaxReturn($json,"json");
    }

    public function add_one()
    {
        $data = I("post.");
        $uuid = uuid();
        
        $data["dept_date"] = time();
        $result = M("performance.bonus_index")->add($data);
        if($result !== false)
        {
            $json["code"] = 0;
            $json["msg"] = "添加成功";
        }
        else
        {
            $json["code"] = -1;
            $json["msg"] = "添加失败";
        }
 
        $this->ajaxReturn($json,"json");
    }

    public function get_col()
    {
        $guide_type = I('post.guide_type');
        $account_date = I('post.account_date');
        $account_date = strtotime($account_date);

        $list = M('performance.set_bonusguide_gather')->where("guide_type = '$guide_type' and account_date = '$account_date' AND sort != 0
                      AND sort IS NOT NULL")->select();
        $col_list = [];
        $item['title'] = "科室";
        $item['code'] = "科室";
        $col_list[] = $item;
        for ($i=0; $i < count($list); $i++) 
        {
            $item['title'] = $list[$i]['guide_name'];
            $item['code'] = $list[$i]['guide_code'];
            $col_list[] = $item;
        }

        $json["data"] = $col_list;
        $json["code"] = 0;
        $json["count"] = 0;
        $json["msg"] = "";
        
        $this->ajaxReturn($json,"json");
    }



}